$(document).ready(function(){
	var VAL ={
		NORMAL     : "0000000"  ,
		EVT_USER   : $("#userId").text(),
		T_FBPBISOPE	 : 'FBPBISOPE',
		T_FBPBISPTH  : 'FBPBISPTH',
        T_FDPAPLYWO  : "FDPAPLYWO",
        T_FPPWHSOPE  : "FPPWHSOPE",
		T_FIPINQSHT  : 'FIPINQSHT',
		T_FSPSKIPOPE : 'FSPSKIPOPE',
		DISABLED_ATTR : {
			'disabled' : true
		},
		ENABLED_ATTR : {
			'disabled' : false
		}
	};
	var globalBean = {
		opeObjs : {},
		getOpeDsc : function(opeID, opeVer) {
			var i, oary;
			for (i = 0; i < this.opeObjs.length; i++) {
				oary = this.opeObjs[i];
				if (typeof (opeVer) === "undefined") {
					if (opeID == oary.ope_id) {
						return oary.ope_dsc;
					}
				} else {
					if (opeID == oary.ope_id && opeVer == oary.ope_ver) {
						return oary.ope_dsc;
					}
				}
			}
		}
	};
	var domObj = {
		    $woIdFk     : $("#woIdFk"),
            $opeIdFk    : $("#opeIdFk"),
            prdIDTxt 	: $("#prdIDTxt"),
            skipOpeIDTxt: $("#skipOpeIDTxt"),
            radioPr     : $("#radioPr"),
            radioWo     : $("#radioWo"),


			prdInfoListDiv : $("#prdInfoListDiv"),
			prdInfoListGrd : $("#prdInfoListGrd"),
			prdInfoListPg : "#prdInfoListPg"
	};
	var btnQuery = {
			query : $("#query_btn"),
			skip : $("#skip_btn")
	};
	
	var controlsQuery = {
		prdIdSpan	: $("#prdIdSpan"),
		woIdSpan    : $("#woIdSpan"),
		mdlIdSpan   : $("#mdlIdSpan"),
		prdStatSpan : $("#prdStatSpan"),
		pathSpan    : $("#pathSpan"),
		pathVerSpan : $("#pathVerSpan"),
		opeDscSpan  : $("#opeDscSpan"),
		opeNoSpan   : $("#opeNoSpan"),
		opeIdSpan   : $("#opeIdSpan"),
		opeVerSpan  : $("#opeVerSpan"),
		porcIdSpan  : $("#porcIdSpan")
	};
	
	var iniGridInfo = function(){
		var prdInfoCM = [
	        {name: 'prd_seq_id',    index: 'prd_seq_id',	label: SHEET_ID_TAG, 	width: 120},
	        {name: 'wo_id_fk',		index: 'wo_id_fk', 		label: WO_ID_TAG,		width: 120},
	        {name: 'mdl_id_fk',    	index: 'mdl_id_fk',   	label: PRODUCT_ID_TAG, 	width: 120},
	        {name: 'path_id_fk',    index: 'path_id_fk',    label: PATH_ID_TAG, 	width: 120},
            {name: 'path_ver_fk',   index: 'path_ver_fk',  label: PATH_VER_TAG, 	width: 120},
	        {name: 'nx_ope_id_fk',  index: 'nx_ope_id_fk',  label: CR_OPE_ID_TAG, 	width: 120}
        ];
		domObj.prdInfoListGrd.jqGrid({
            url:"",
            datatype:"local",
            mtype:"POST",
            height:425,
            width:domObj.prdInfoListDiv.width(),
            autowidth:true,
            shrinkToFit:false,
            autoScroll: false,
            scroll:true,
            rownumWidth : true,
            resizable : true,
            rowNum:40,
            loadonce:true,
            fixed:true,
            viewrecords:true,
            multiselect : true,
            pager : domObj.prdInfoListPg,
            colModel: prdInfoCM,
            onSelectRow : function(rowId) {
                var rowData = domObj.prdInfoListGrd.jqGrid("getRowData", rowId);
                var prd_seq_Id = rowData.prd_seq_id;
                var inTrxObj ={
                    trx_id     : VAL.T_FIPINQSHT,
                    action_flg : 'Q'        ,
                    prd_seq_id : prd_seq_Id
                };
                var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
                if(  outTrxObj.rtn_code == VAL.NORMAL ) {
                    clearSpanFnc();
                    controlsQuery.prdIdSpan.html(outTrxObj.prd_seq_id);
                    controlsQuery.woIdSpan.html(outTrxObj.wo_id_fk);
                    controlsQuery.mdlIdSpan.html(outTrxObj.mdl_id_fk);
                    controlsQuery.prdStatSpan.html(outTrxObj.prd_stat);
                    controlsQuery.pathSpan.html(outTrxObj.path_id_fk);
                    controlsQuery.pathVerSpan.html(outTrxObj.path_ver_fk);
                    controlsQuery.opeDscSpan.html(globalBean.getOpeDsc(outTrxObj.ope_id_fk));
                    controlsQuery.opeNoSpan.html(outTrxObj.ope_no_fk);
                    controlsQuery.opeIdSpan.html(outTrxObj.ope_id_fk);
                    controlsQuery.opeVerSpan.html(outTrxObj.ope_ver_fk);
                    controlsQuery.porcIdSpan.html(outTrxObj.proc_id_fk);
                }
            },
            onSelectAll: function(aRowids) {
                var rowData = domObj.prdInfoListGrd.jqGrid("getRowData", aRowids[0]);
                var prd_seq_Id = rowData.prd_seq_id;
                var inTrxObj ={
                    trx_id     : VAL.T_FIPINQSHT,
                    action_flg : 'Q'        ,
                    prd_seq_id : prd_seq_Id
                };
                var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
                if(  outTrxObj.rtn_code == VAL.NORMAL ) {
                    clearSpanFnc();
                    controlsQuery.prdIdSpan.html(outTrxObj.prd_seq_id);
                    controlsQuery.woIdSpan.html(outTrxObj.wo_id_fk);
                    controlsQuery.mdlIdSpan.html(outTrxObj.mdl_id_fk);
                    controlsQuery.prdStatSpan.html(outTrxObj.prd_stat);
                    controlsQuery.pathSpan.html(outTrxObj.path_id_fk);
                    controlsQuery.pathVerSpan.html(outTrxObj.path_ver_fk);
                    controlsQuery.opeDscSpan.html(globalBean.getOpeDsc(outTrxObj.ope_id_fk));
                    controlsQuery.opeNoSpan.html(outTrxObj.ope_no_fk);
                    controlsQuery.opeIdSpan.html(outTrxObj.ope_id_fk);
                    controlsQuery.opeVerSpan.html(outTrxObj.ope_ver_fk);
                    controlsQuery.porcIdSpan.html(outTrxObj.proc_id_fk);
                }
            }
        });
	};
	var toolFunc = {
        iniOpeInfo : function(){
            var inObj, outObj;
            inObj = {
                trx_id : VAL.T_FBPBISOPE,
                action_flg : 'L'
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                globalBean.opeObjs = outObj.oary;
            }
        },
        iniWoIdSel :function(){
            var inObj = {
                trx_id : VAL.T_FDPAPLYWO,
                action_flg : "Q",
                iary : []
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                var oaryA = outObj.oary;
                var compare = function (obj1, obj2) {
                    var val1 = obj1.wo_id;
                    var val2 = obj2.wo_id;
                    if (val1 < val2) {
                        return -1;
                    } else if (val1 > val2) {
                        return 1;
                    } else {
                        return 0;
                    }
                }
                oaryA.sort(compare);
                SelectDom.addSelectArr(domObj.$woIdFk, oaryA, "wo_id","wo_id","",true);
            }
        },
        iniOpeIdSel:function(){
            var woIdFk = $.trim(domObj.$woIdFk.val());
            domObj.$opeIdFk.empty();
            if (!woIdFk) {
                return false;
            }
            var iary={
                wo_id:woIdFk
            }
            var inObj = {
                trx_id : VAL.T_FDPAPLYWO,
                action_flg : "Q",
                iary : [iary]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                var oary = outObj.oary[0];
                toolFunc.setPathInfo(oary.path_id_fk,oary.path_ver_fk);
            }
        },
        /*iniOpeIdSel:function(){
            var inObj = {
                trx_id : VAL.T_FBPBISOPE,
                action_flg : "Q",
                iary : [{ope_id:undefined}]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                var oaryA = outObj.oary;
                var compare = function (obj1, obj2) {
                    var val1 = obj1.ope_id;
                    var val2 = obj2.ope_id;
                    if (val1 < val2) {
                        return -1;
                    } else if (val1 > val2) {
                        return 1;
                    } else {
                        return 0;
                    }
                }
                oaryA.sort(compare);
                SelectDom.addSelectArr_(domObj.$opeIdFk, oaryA, "ope_id","ope_id","ope_dsc",true);
            }
        },*/
		setOpeSelectDate : function(dataCnt, arr, selVal, selVal2, selTxt, queryObj,emptySel){
            var i, realCnt;
            queryObj.empty();
            realCnt = parseInt( dataCnt, 10);
            if (emptySel) {
					queryObj.append("<option value=''></option>");
			}
            if( realCnt > 0 ){
            	for(var j=0; j<realCnt;j++ ){
                    arr[j].ope_dsc = globalBean.getOpeDsc(arr[j][selVal]);
				}
                /*var compare = function (obj1, obj2) {
                    var val1 = obj1.ope_dsc;
                    var val2 = obj2.ope_dsc;
                    if (val1 < val2) {
                        return -1;
                    } else if (val1 > val2) {
                        return 1;
                    } else {
                        return 0;
                    }
                }
                arr.sort(compare);*/
                for( i = 0; i < realCnt; i++ ){
                	// var ope_dsc = globalBean.getOpeDsc(arr[i][selVal]);
                    queryObj.append("<option value="+ arr[i][selVal] + "@" + arr[i][selVal2] + "@" + arr[i][selTxt] +">"+ arr[i][selVal] + ":"+ arr[i].ope_dsc +"</option>");
                }
            }
            queryObj.select2({
    	    	theme : "bootstrap"
    	    });
        },
		setPathInfo : function(path_id,path_ver){
		    var iaryB = {};
		    iaryB.path_id_fk  = path_id;
		    iaryB.path_ver_fk = path_ver;
			var inObj = {
					trx_id	   : VAL.T_FBPBISPTH,
					action_flg : "I",
                	iaryB      : [iaryB]
				};
            var outObj = comTrxSubSendPostJson(inObj);
				if (outObj.rtn_code == VAL.NORMAL) {
                    var oaryB = outObj.oaryB;
					toolFunc.setOpeSelectDate(outObj.tbl_cnt_b, oaryB, "cr_ope_id_fk", "cr_ope_ver_fk", "cr_ope_no", domObj.skipOpeIDTxt,true);
                    toolFunc.setOpeSelectDate(outObj.tbl_cnt_b, oaryB, "cr_ope_id_fk", "cr_ope_ver_fk", "cr_ope_no", domObj.$opeIdFk,true);
				}
		}
	}
	var btnFunc = {
		query_func :function(){
            var inObj,outObjs;
            if(domObj.radioPr.attr("checked") == "checked"){//单片跳站
                var prdSeqId = $.trim(domObj.prdIDTxt.val());// 玻璃id
                if(!prdSeqId){
                    showErrorDialog("", "屏体代码不能为空");
                    return false;
                }
            }else{//工单跳站
                var woIdFk   = $.trim(domObj.$woIdFk.val());
                var opeIdFk  = $.trim(domObj.$opeIdFk.val());
                var ope_id   = opeIdFk.split("@")[0];
                var ope_ver  = opeIdFk.split("@")[1];
                var ope_no   = opeIdFk.split("@")[2];
                if (!woIdFk) {
                    showErrorDialog("", "工单代码不能为空");
                    return false;
                }
                if (!ope_id) {
                    showErrorDialog("", "站点代码不能为空");
                    return false;
                }
            }
            inObj = {
                trx_id : VAL.T_FIPINQSHT,
                action_flg : "M",
                prd_seq_id : prdSeqId,
                wo_id:woIdFk,
                ope_id:ope_id
            };
            outObjs = comTrxSubSendPostJson(inObj);
            if (outObjs.rtn_code === VAL.NORMAL) {
                if(outObjs.oary2.length>0){
                    var oary = outObjs.oary2[0];
                    if(domObj.radioPr.attr("checked") == "checked"){//单片跳站
                        if(oary.prd_stat!="WAIT"){
                            showErrorDialog("", "["+oary.prd_seq_id+"]屏体状态为["+oary.prd_stat+"]");
                            return false;
                        }
                        if(oary.path_id_fk=="REWFMA01"){
                            showErrorDialog("", "["+oary.prd_seq_id+"]屏体在FMA站点不允许跳站");
                            return false;
                        }
                        var data =  domObj.prdInfoListGrd.jqGrid("getRowData");
                        if(data.length>0){
                            for(var i=0; i<data.length;i++){
                                if(oary.prd_seq_id==data[i].prd_seq_id){
                                    showErrorDialog("", "该屏体已存在列表中");
                                    return false;
                                }
                            }
                            if(oary.path_id_fk!=data[0].path_id_fk){
                                showErrorDialog("", "工艺路线不匹配");
                                return false;
                            }
                            if(oary.path_ver_fk!=data[0].path_ver_fk){
                                showErrorDialog("", "路线版本不匹配");
                                return false;
                            }
                            if(oary.nx_ope_id_fk!=data[0].nx_ope_id_fk){
                                showErrorDialog("", "站点不匹配");
                                return false;
                            }
                            var newRowId = getGridNewRowID("#prdInfoListGrd");
                            domObj.prdInfoListGrd.jqGrid("addRowData", newRowId, oary);
                            domObj.prdIDTxt.val("");
                        }else{
                            var newRowId = getGridNewRowID("#prdInfoListGrd");
                            domObj.prdInfoListGrd.jqGrid("addRowData", newRowId, oary);
                            domObj.prdIDTxt.val("");
                        }
                        toolFunc.setPathInfo(oary.path_id_fk,oary.path_ver_fk);
                    }else{//工单跳站
                        var oary=[];
                        for(var i=0;i<outObjs.oary2.length;i++){
                            if(outObjs.oary2[i].prd_stat=="WAIT"){
                                oary.push(outObjs.oary2[i]);
                            }
                        }
                        setGridInfo(oary,"#prdInfoListGrd");
                    }
                }
			}
		},
		skip_func : function(){
            var ids = domObj.prdInfoListGrd.jqGrid('getGridParam','selarrrow');
            if(ids.length <= 0){
                showErrorDialog("","请选择需要跳站的屏体！")
                return false;
            }
            var iary = [];
            for(var i = 0 ; i < ids.length ; i++){
                var rowData = domObj.prdInfoListGrd.jqGrid("getRowData",ids[i]);
                var iaryA = {
                    prd_seq_id :rowData.prd_seq_id
                }
                iary.push(iaryA);
            }
			var skipOpeId = domObj.skipOpeIDTxt.val();
			if(!skipOpeId){
				showErrorDialog("","请选择需要转入的站点！");
				return;
			}
			//获取转入站点
			var ope_id = skipOpeId.split("@")[0];
			var ope_ver = skipOpeId.split("@")[1];
            var ope_no = skipOpeId.split("@")[2];

			var path_id = controlsQuery.pathSpan.html();
			var path_ver = controlsQuery.pathVerSpan.html();
			var rows = domObj.prdInfoListGrd.jqGrid("getRowData");
			if (!rows) {
				showErrorDialog("","玻璃信息列表不能为空");
				return;
			}
			var inObj = {
					trx_id : VAL.T_FSPSKIPOPE,
					action_flg : 'J',
					evt_usr : VAL.EVT_USER,
					nx_ope_id_fk : ope_id,
					nx_ope_ver_fk: ope_ver,
					nx_ope_no_fk : ope_no,
					nx_path_id_fk: path_id,
					nx_path_ver_fk : path_ver,
					iary :iary
				};
			var outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code == VAL.NORMAL) {
				showSuccessDialog("跳站成功");
                for(var i = 0 ; i < ids.length ; i++){
                    var rowData = domObj.prdInfoListGrd.jqGrid("getRowData",ids[i]);
                        rowData.nx_ope_id_fk= ope_id;
                    domObj.prdInfoListGrd.jqGrid('setRowData', ids[i], rowData);
                }
			}
		}
	};
	
	var iniButtonAction = function(){
		btnQuery.query.click(function(){
			btnFunc.query_func();
		});
		btnQuery.skip.click(function(){
			btnFunc.skip_func();
		});
        domObj.prdIDTxt.keydown(function(event){
		    if(event.keyCode==13){
		    	btnFunc.query_func();
		    }
		});
        domObj.$woIdFk.change(function() {
            toolFunc.iniOpeIdSel();
        });
        $(":radio").click(function(){
            if(domObj.radioPr.attr("checked") == "checked"){
                $("#prdInfoListGrd").jqGrid('clearGridData');
                domObj.prdIDTxt.val("");
                SelectDom.initWithSpace(domObj.$woIdFk);
                SelectDom.initWithSpace(domObj.$opeIdFk);
                SelectDom.initWithSpace(domObj.skipOpeIDTxt);
                domObj.prdIDTxt.attr(VAL.ENABLED_ATTR);
                domObj.$woIdFk.attr(VAL.DISABLED_ATTR);
                domObj.$opeIdFk.attr(VAL.DISABLED_ATTR);
            }
            if(domObj.radioWo.attr("checked") == "checked"){
                $("#prdInfoListGrd").jqGrid('clearGridData');
                domObj.prdIDTxt.val("");
                SelectDom.initWithSpace(domObj.$woIdFk);
                SelectDom.initWithSpace(domObj.$opeIdFk);
                SelectDom.initWithSpace(domObj.skipOpeIDTxt);
                domObj.prdIDTxt.attr(VAL.DISABLED_ATTR);
                domObj.$woIdFk.attr(VAL.ENABLED_ATTR);
                domObj.$opeIdFk.attr(VAL.ENABLED_ATTR);
                toolFunc.iniOpeIdSel();
                toolFunc.iniWoIdSel();
            }
        })
	};
	
	function clearSpanFnc() {
        controlsQuery.prdIdSpan.html("");
        controlsQuery.woIdSpan.html("");
        controlsQuery.mdlIdSpan.html("");
        controlsQuery.prdStatSpan.html("");
        controlsQuery.pathSpan.html("");
        controlsQuery.pathVerSpan.html("");
        controlsQuery.opeDscSpan.html("");
        controlsQuery.opeNoSpan.html("");
        controlsQuery.opeIdSpan.html("");
        controlsQuery.opeVerSpan.html("");
        controlsQuery.porcIdSpan.html("");
    }
	
	var initializationFunc = function(){
		$("form").submit(function(){
            return false;
        });
		iniGridInfo();
		iniButtonAction();
		$("select").empty();
		clearSpanFnc();
        toolFunc.iniOpeInfo();
		toolFunc.iniOpeIdSel();
        toolFunc.iniWoIdSel();
	};
	
	initializationFunc();
    //enter
   /*document.onkeydown=function(event){
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if(e && e.keyCode==13){ // 按 enter
            $('.bootbox').modal("hide");
        }
    }*/
    //表格自适应
    function resizeFnc(){
        var offsetBottom, divWidth;
        divWidth = $("#prdInfoListDiv").width();
        offsetBottom = $(window).height() - $("#prdInfoListDiv").offset().top;
        $("#prdInfoListDiv").height(offsetBottom * 0.95);
        $("#prdInfoListGrd").setGridWidth(divWidth * 0.99);
        $("#prdInfoListGrd").setGridHeight(offsetBottom * 0.99 - 100);
    };

    resizeFnc();
    $(window).resize(function() {
        resizeFnc();
    });

});